﻿using FlexFlow.Model;
using FlexFlow.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FlexFlow.MVC.Controllers
{
    /// <summary>
    /// 表示表单API控制器
    /// </summary>
    [Auth(MVC.AuthType.API)]
    public class FormAPIController : Controller
    {
        /// <summary>
        /// 创建表单
        /// </summary>
        /// <param name="name">表单名称</param>
        /// <param name="templateId">表单模板ID</param>
        /// <returns>操作结果</returns>
        public ActionResult Create(string name, Guid templateId)
        {
            LoginedUserInfo uinfo = DefaultComponentSettings.OnlineUserPool.GetLoginedUserInfo(Request);
            return Json(WorkFlowFormManager.Default.Create(name, templateId, uinfo.UserId));
        }
        /// <summary>
        /// 取得指定表单
        /// </summary>
        /// <param name="formId">
        /// 表单ID
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public ActionResult GetById(Guid formId)
        {
            OperateResult formGetRes = WorkFlowFormManager.Default.GetById(formId, false);
            if (formGetRes.ResultCode != 200)
	        {
                return Json(formGetRes);
	        }
            WorkFlowForm form = formGetRes.Tag as WorkFlowForm;
            OperateResult tempGetRes = FormTemplateManager.Default.GetByID((formGetRes.Tag as WorkFlowForm).Template);
            if (tempGetRes.ResultCode != 200)
	        {
                return Json(tempGetRes);
	        }
            FormTemplate temp = tempGetRes.Tag as FormTemplate;
            return Json(new{
                id = form.Identity,
                name = form.Name,
                tempName = temp.Name
            });
        }
        /// <summary>
        /// 更新指定ID对应的表单模板的内容
        /// </summary>
        /// <param name="formId">
        /// 表单模板ID
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public ActionResult UpdateContent(Guid formId)
        {
            Dictionary<string, object> frmDic = new Dictionary<string, object>();
            Request.Form.CopyTo(frmDic);
            OperateResult res = WorkFlowFormManager.Default.UpdateContent(formId, frmDic);
            return Json(res);
        }
        /// <summary>
        /// 移除表单
        /// </summary>
        /// <param name="formId">
        /// 表单ID
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public ActionResult Remove(Guid formId)
        {
            return Json(WorkFlowFormManager.Default.Remove(formId));
        }
        /// <summary>
        /// 取得指定用户创建的并且名称包含指定字符的表单列表
        /// </summary>
        /// <param name="nameLike">
        /// 要取得的表单名称中应包含的字符列表
        /// </param>
        /// <param name="limit">
        /// 返回的数据数量限制
        /// </param>
        /// <param name="tempId">
        /// 表单对应的表单模板ID
        /// </param>
        /// <param name="skip">
        /// 返回的数据的起始位置
        /// </param>
        /// <returns>
        /// 表单列表
        /// </returns>
        public ActionResult GetUserForms(string nameLike = "", Guid? tempId = null, int skip = 0, int limit = 10)
        {
            LoginedUserInfo uinfo = DefaultComponentSettings.OnlineUserPool.GetLoginedUserInfo(Request);
            OperateResult opres = WorkFlowFormManager.Default.GetUserForms(uinfo.UserId, nameLike, tempId, skip, limit);
            if (opres.ResultCode == 200)
            {
                List<WorkFlowForm> forms = opres.Tag as List<WorkFlowForm>;
                List<dynamic> resList = new List<dynamic>();
                foreach (var form in forms)
                {
                    OperateResult tempGetRes = FormTemplateManager.Default.GetByID(form.Template);
                    if (tempGetRes.ResultCode == 200 && tempGetRes.Tag != null)
                    {
                        resList.Add(new
                        {
                            id = form.Identity,
                            name = form.Name,
                            createTime = form.CreateTime.ToString(),
                            template = tempGetRes.Tag,
                            tag = form.Tag
                        });
                    }
                }
                return Json(new
                {
                    draw = 1,
                    recordsTotal = resList.Count,
                    recordsFiltered = resList.Count,
                    data = resList
                });
            }
            return Json(opres);
        }
        /// <summary>
        /// 取得指定用户创建的并且名称包含指定字符的表单列表
        /// </summary>
        /// <param name="nameLike">
        /// 要取得的表单名称中应包含的字符列表
        /// </param>
        /// <param name="limit">
        /// 返回的数据数量限制
        /// </param>
        /// <param name="tempId">
        /// 表单对应的表单模板ID
        /// </param>
        /// <param name="skip">
        /// 返回的数据的起始位置
        /// </param>
        /// <returns>
        /// 表单列表
        /// </returns>
        public ActionResult QueryUserFormByName(string nameLike = "", Guid? tempId = null, int skip = 0, int limit = 10)
        {
            LoginedUserInfo uinfo = DefaultComponentSettings.OnlineUserPool.GetLoginedUserInfo(Request);
            OperateResult opres = WorkFlowFormManager.Default.GetUserForms(uinfo.UserId, nameLike, tempId, skip, limit);
            if (opres.ResultCode == 200)
            {
                List<WorkFlowForm> forms = opres.Tag as List<WorkFlowForm>;
                List<dynamic> resList = new List<dynamic>();
                foreach (var form in forms)
                {
                    OperateResult tempGetRes = FormTemplateManager.Default.GetByID(form.Template);
                    if (tempGetRes.ResultCode == 200 && tempGetRes.Tag != null)
                    {
                        resList.Add(new
                        {
                            id = form.Identity,
                            name = form.Name,
                            createTime = form.CreateTime.ToString(),
                            template = tempGetRes.Tag,
                            tag = form.Tag
                        });
                    }
                }
                return Json(resList,JsonRequestBehavior.AllowGet);
            }
            return Json(opres);
        }
    }
}
